package com.xinshujia.controller;

import com.xinshujia.config.MqttConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-25 15:36       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 */
@RestController
@RequestMapping("test")
public class MqttSendController {
    private final MqttConfig mqttConfig;

    public MqttSendController(MqttConfig mqttConfig) {
        this.mqttConfig = mqttConfig;
    }

    @GetMapping("/sendMessage")
    public String sendMessage(@RequestParam("topic") String topic,
                              @RequestParam("payload") String payload) {
        boolean publish = mqttConfig.publish(topic, payload); //发布消息
        mqttConfig.subscribe(topic, 2); //订阅信息
        return publish ? "OK" : "no";
    }
}
